package com.ysd.servlet.admin;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.google.gson.Gson;
import com.ysd.dao.impl.FishClassifyDaoImpl;
import com.ysd.entity.Admin;
import com.ysd.entity.FishClassify;
import com.ysd.util.BaseServlet;
import com.ysd.util.Result;


public class AdminProductCategoryServlet extends BaseServlet {
	
	/**
	 * 获取产品分类
	 * localhost:8080/shangcheng1/adminProductCategory?reqName=getProductCate
	 * @param request
	 * @param response
	 * @throws ServletException
	 * @throws IOException
	 */
	
	public void getProductCate(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		//任何一个servlet中的任何一个方法都应该写session
		/*
		 * HttpSession session = request.getSession();
		Admin admin = (Admin)session.getAttribute("admin");
		if (admin == null) {   为空表示为登录过,返回重新登录
			
		} else {   

		}
		*/
		
		FishClassifyDaoImpl fcDao = new FishClassifyDaoImpl();
		
		int page = Integer.parseInt(request.getParameter("page"));
		int size = Integer.parseInt(request.getParameter("size"));
		List<FishClassify> fcList = fcDao.getProCateByPage(page, size);
		
		response.setCharacterEncoding("UTF-8");
		PrintWriter out = response.getWriter();//创建out对象
		
		Gson gson = new Gson();//创建gson对象
		
		String res = gson.toJson(fcList);//将fcList转化为json对象
		out.print(res);//响应
		
		out.flush();
		out.close();
		
	}
	
	/**
	 * 获取产品分类
	 * localhost:8080/shangcheng1/adminProductCategory?reqName=getAllProductCate
	 * @param request
	 * @param response
	 * @throws ServletException
	 * @throws IOException
	 */
	
	public void getAllProductCate(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		
		FishClassifyDaoImpl fcDao = new FishClassifyDaoImpl();
		
		List<FishClassify> fcList = fcDao.getFish();
		
		response.setCharacterEncoding("UTF-8");
		PrintWriter out = response.getWriter();//创建out对象
		
		Gson gson = new Gson();//创建gson对象
		
		String res = gson.toJson(fcList.size());//通过gson对象的toJson方法将fcList转化为json对象
		out.print(res);//响应
		
		out.flush();
		out.close();
		
	}
	
	/**
	 * 根据Id删除数据
	 * localhost:8080/shangcheng1/adminProductCategory?reqName=delProductCate
	 * @param request
	 * @param response
	 * @throws ServletException
	 * @throws IOException
	 */
	public void delProductCate(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		
		int fishId = Integer.parseInt(request.getParameter("fishClassifyId"));
		FishClassifyDaoImpl FcDao = new FishClassifyDaoImpl();
		int n = FcDao.getdelProductCatbyId(fishId);//调用实现类的删除方法
		
		response.setCharacterEncoding("UTF-8");
		PrintWriter out = response.getWriter();//创建out对象
		
		if (n > 0) {//删除成功
			out.print(Result.toClient(true, "删除成功", ""));
		} else {//删除失败
			out.print(Result.toClient(false, "删除失败", ""));
		}
		
		out.flush();
		out.close();
		
	}
	
	/**
	 * 根据产品名称添加数据
	 * localhost:8080/shangcheng1/adminProductCategory?reqName=addProductCate
	 * @param request
	 * @param response
	 * @throws ServletException
	 * @throws IOException
	 */
	public void addProductCate(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		response.setCharacterEncoding("UTF-8");
		PrintWriter out = response.getWriter();
		FishClassifyDaoImpl fishDao = new FishClassifyDaoImpl();
		
		String fishClassifyName = request.getParameter("classifyName");
		if(fishClassifyName!=null && !fishClassifyName.equals("")) {
			List<FishClassify> flist = fishDao.getProCateWhereName(fishClassifyName);
			if (flist.size()==0) {
				FishClassify f = new FishClassify(0, fishClassifyName);
				
				int n = fishDao.addProductClassify(f);
				
				if (n > 0) {
					out.print(Result.toClient(true, "添加成功", ""));
				} else {
					out.print(Result.toClient(false, "添加失败", ""));
				}
			} else {
				out.print(Result.toClient(false, "分类名已存在", ""));
			}
		}
		
		out.flush();
		out.close();
		
	}
	
	/**
	 * 根据产品id查询产品
	 * localhost:8080/shangcheng1/adminProductCategory?reqName=selProductCate
	 * @param request
	 * @param response
	 * @throws ServletException
	 * @throws IOException
	 */
	public void selProductCate(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		response.setCharacterEncoding("UTF-8");
		PrintWriter out = response.getWriter();
		
		FishClassifyDaoImpl fishDao = new FishClassifyDaoImpl();
		
		int fishId = Integer.parseInt(request.getParameter("fishClassifyId"));
		FishClassify fc = fishDao.getfishCateById(fishId);
		
		Gson gson = new Gson();
		String res = gson.toJson(fc);
		out.print(res);
		
		out.flush();
		out.close();
		
	}
	
	/**
	 * 根据产品id查询产品
	 * localhost:8080/shangcheng1/adminProductCategory?reqName=updateProductCate
	 * @param request
	 * @param response
	 * @throws ServletException
	 * @throws IOException
	 */
	public void updateProductCate(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		response.setCharacterEncoding("UTF-8");
		PrintWriter out = response.getWriter();
		
		FishClassifyDaoImpl fishDao = new FishClassifyDaoImpl();
		
		int classifyId = Integer.parseInt(request.getParameter("proCateId"));
		String fishName = request.getParameter("proCateName");
		
		if(fishName!=null && !fishName.equals("")) {
			List<FishClassify> fishList =  fishDao.getProCateWhereName(fishName);
			System.out.println(fishList);
			if (fishList.size()==0) {
				FishClassify fishc = new FishClassify(classifyId, fishName);
				System.out.println(fishc);
				int n = fishDao.updateFishCate(fishc);
				
				if (n > 0) {
					out.print(Result.toClient(true, "修改成功", ""));
				} else {
					out.print(Result.toClient(false, "修改失败", ""));
				}
			} else {
				out.print(Result.toClient(false, "分类已存在", ""));
			}
		}
		
		
		
		out.flush();
		out.close();
		
	}
	
	/**
	 * 根据多条产品分类id删除产品
	 * localhost:8080/shangcheng1/adminProductCategory?reqName=delProductCateByIds
	 * @param request
	 * @param response
	 * @throws ServletException
	 * @throws IOException
	 */
	public void delProductCateByIds(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		response.setCharacterEncoding("UTF-8");
		PrintWriter out = response.getWriter();
		
		FishClassifyDaoImpl fishDao = new FishClassifyDaoImpl();
		String ids = request.getParameter("ids");
		int n = fishDao.delFishCateByIds(ids);
		
				
			if (n > 0) {
				out.print(Result.toClient(true, "修改成功", ""));
			} else {
				out.print(Result.toClient(false, "修改失败", ""));
			}
			
			out.flush();
			out.close();

	}
		
}
